package com.bocloud.cmp.dao.app.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bocloud.cmp.dao.app.OpenshiftClusterDao;
import com.bocloud.cmp.entity.app.resource.OpenshiftCluster;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.ListTool;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.database.utils.SQLHelper;

/**
 * @author Zaney
 * @data:2017年3月28日
 * @describe: openshift集群dao实现类
 */
@Repository("openshiftClusterDao")
public class OpenshiftClusterDaoImpl extends JdbcGenericDao<OpenshiftCluster, Long> implements OpenshiftClusterDao {

	@Override
	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from openshift_cluster a where a.is_deleted = 0";
		sql = SQLHelper.buildRawSql(sql, params, null, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	@Override
	public List<OpenshiftCluster> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = "select * from openshift_cluster a where a.is_deleted = 0";
		sql = SQLHelper.buildRawSql(sql, page, rows, params, sorter, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.list(OpenshiftCluster.class, sql, paramMap);
	}

	@Override
	public OpenshiftCluster query(Long id) throws Exception {
		String sql = "select * from openshift_cluster where is_deleted = 0 and id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<OpenshiftCluster> list = this.list(OpenshiftCluster.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return this.get(OpenshiftCluster.class, id);
	}

	@Override
	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name from openshift_cluster a where a.is_deleted = 0";
		sql = SQLHelper.buildSql(sql, 1, Integer.MAX_VALUE, params, sorter, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		List<OpenshiftCluster> clusters = this.list(OpenshiftCluster.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (OpenshiftCluster cluster : clusters) {
			beans.add(new SimpleBean(cluster.getId(), cluster.getName()));
		}
		return beans;
	}

	@Override
	public boolean deleteById(Long id, Long userId) throws Exception {
		String sql = "update openshift_cluster set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	@Override
	public List<SimpleBean> listOpenshiftClusterByAppId(Long appId) throws Exception {
		String sql = "select oc.id,oc.name from openshift_cluster oc, application_openshift_cluster_info aoc"
				+ " where oc.is_deleted = 0 and aoc.application_id = :app_id";
		Map<String, Object> param = MapTools.simpleMap("app_id", appId);
		List<OpenshiftCluster> clusters = this.list(OpenshiftCluster.class, sql, param);
		List<SimpleBean> beans = new ArrayList<>();
		if (ListTool.isEmpty(clusters)) {
			beans = this.list(new ArrayList<>(), null);
		} else {
			for (OpenshiftCluster cluster : clusters) {
				beans.add(new SimpleBean(cluster.getId(), cluster.getName()));
			}
		}
		return beans;
	}

	@Override
	public OpenshiftCluster getOpenshiftClusterByproxyUrl(String proxyUrl) throws Exception {
		String sql = "select * from openshift_cluster where is_deleted = 0 and proxy_url = :proxyUrl";
		Map<String, Object> params = MapTools.simpleMap("proxyUrl", proxyUrl);
		List<OpenshiftCluster> list = this.list(OpenshiftCluster.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<OpenshiftCluster> selectAllCluster() throws Exception {
		String sql = "select * from openshift_cluster where is_deleted = 0 ";
		return this.list(OpenshiftCluster.class, sql);
	}

	@Override
	public OpenshiftCluster checkName(String name) throws Exception {
		String sql = "select * from openshift_cluster where is_deleted = 0 and name = :name";
		Map<String, Object> params = MapTools.simpleMap("name", name);
		List<OpenshiftCluster> list = this.list(OpenshiftCluster.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

}
